package com.apes.scm.masterdata.stock.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.enumeration.model.EnumerationType;
import com.apes.scm.masterdata.enumeration.repository.EnumerationRepository;
import com.apes.scm.masterdata.enumeration.repository.EnumerationTypeRepository;
import com.apes.scm.masterdata.stock.model.StockUsage;
import com.apes.scm.masterdata.stock.repository.StockUsageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * 功能：库区属性
 *
 * @author HuJianlin
 * @create 2018-03-14 9:45
 */
@Service("usageService")
public class UsageService extends DomainService {

    @Autowired
    public StockUsageRepository usageRepository;
    @Autowired
    public EnumerationRepository enumerationRepository;
    @Autowired
    public EnumerationTypeRepository enumerationTypeRepository;

    public StockUsage save(SimpleRequest request) {
        StockUsage usage = request.getO(StockUsage.class);
        return usageRepository.saveAndFlush(usage);
    }

    /**
     * 检查数据判断，判读当前的输入版本信息
     *
     * @param request
     */
    public void checkData(SimpleRequest request) {
        String id = request.get("id");
        int version = request.get("version");

        // 从数据库中取数，检查数据
        StockUsage oldStockUsage = usageRepository.findOne(id);
        if (oldStockUsage == null) throw new RuntimeException("招聘申请单【" + id + "】在系统中不存在。");
        if (oldStockUsage.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");
    }

    public StockUsage update(SimpleRequest request) {
        //检查数据
        checkData(request);

        StockUsage usage = request.getO(StockUsage.class);
        return usageRepository.saveAndFlush(usage);
    }

    /**
     * 功能：查询库区属性
     *
     * @param request
     */
    public Page<StockUsage> findAll(SimpleRequest request) {
        return usageRepository.findAll(PageRequest.of(request.get("page"), request.get("size")));
    }

    public String delete(SimpleRequest request) throws Exception {
//        Dept dept = request.getO(Dept.class);
//        deptRepository.delete(dept);

        String id = request.get("id");
        int versions = request.get("version");

        StockUsage usage = usageRepository.findOne(id);
        if (usage == null) throw new RuntimeException("部门【" + id + "】在系统中不存在。");
        if (usage.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        usageRepository.delete(usage);
        return "删除成功";
    }

    /**
     * 查找单据类型
     *
     * @param request
     * @return
     */
    public Page<Enumeration> findOrderType(SimpleRequest request) {
        EnumerationType enumerationType = enumerationTypeRepository.findOne("orderType");
        return enumerationRepository.findByType(enumerationType, PageRequest.of(request.get("page"), request.get("size")));
    }

    /**
     * 功能：查询库区属性 基础的九种
     *
     * @param request
     */
    public Page<StockUsage> findByBase(SimpleRequest request) {
        return usageRepository.findByBase(PageRequest.of(request.get("page"), request.get("size")));
    }

}
